package com.llkj.qingnianguoxue.circle;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.RxBusChatBean;
import com.llkj.qingnianguoxue.bean.eventbean.MemberManagerEvent;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DialogUtil;
import com.llkj.qingnianguoxue.util.KeyBoardUtil;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.sevenheaven.iosswitch.ShSwitchView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MemberManagerActivity extends BaseActivity {

    @BindView(R.id.tv_member_contributions)
    protected TextView mTvMemberContributions;//缴费金额
    @BindView(R.id.tv_member_duration)
    protected TextView mTvmemberDuration;//费用时长
    @BindView(R.id.swNotificationOpen)
    protected ShSwitchView mShSwitchView;//到期通知开关
    private int mModityOption = -1;//1费用时长，0默认金额
    private String mDayDuration,mContributionsMoney;
    private String mGroupChatId,mCircleId;//圈子聊天id，圈子id
    private boolean mIsPush = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_manager);
        setTitle("会费管理", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_TEXT,"保存");
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        tvRight.setTextColor(ContextCompat.getColor(this, R.color.blue3989ff));
        ButterKnife.bind(this);

        getIntentData();
        initView();
    }

    private void initView(){
        mTvMemberContributions.setText(mContributionsMoney+"银子");
        mTvmemberDuration.setText(mDayDuration+"天");
        mShSwitchView.setOn(mIsPush);
        mShSwitchView.setOnSwitchStateChangeListener(new ShSwitchView.OnSwitchStateChangeListener() {
            @Override
            public void onSwitchStateChange(boolean isOn) {
                mIsPush = isOn;
            }
        });
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        modifyCircleData();
    }

    @OnClick({R.id.rlMemberContributions, R.id.rlMemberDuration, R.id.rlContributionsList})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.rlMemberContributions://缴费金额
                mModityOption = 0;
                setModifyContent(getString(R.string.member_manager_contributions),"",-1);
                break;
            case R.id.rlMemberDuration://费用时限
                mModityOption = 1;
                setModifyContent(getString(R.string.member_manager_duration),"", InputType.TYPE_CLASS_NUMBER);
                break;
            case R.id.rlContributionsList://缴费明细
                Bundle bundle = new Bundle();
                bundle.putString("groupChatId",mGroupChatId);
                startActivity(ContributionsListActivity.class,bundle);
                break;
        }
    }

    private void setModifyContent(String title, String content, int inputType){
        Dialog dialog = DialogUtil.modifyGroupDialog(this,inputType, title, content, new DialogUtil.DialogRechargeClickListener() {
            @Override
            public void rechargeClick(int type, String content) {
                KeyBoardUtil.hideSoftKeyboard(MemberManagerActivity.this);
                if(type==1){
                    if(mModityOption==1){
                        mDayDuration = content;
                        mTvmemberDuration.setText(content+"天");
                    }else{
                        mContributionsMoney = content;
                        mTvMemberContributions.setText(content+"银子");
                    }
                }
            }
        });
        final EditText editText = (EditText) dialog.getWindow().findViewById(R.id.tv_modify_content);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String content = charSequence.toString();
                if(mModityOption==1){
                    if(content.length()>1 && content.startsWith("0")){//判断首位为0则截取
                        content = content.length()>1?content.substring(1,content.length()):"";
                        editText.setText(content);
                        editText.setSelection(content.length());
                    }
                }else{
                    int position = content.indexOf(".");
                    if(position >0 && content.length()-position>3){//判断小数点后位数，大于2位，则截取
                        content = content.substring(0,position+3);
                        editText.setText(content);
                        editText.setSelection(position+3);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {}
        });
        if(mModityOption==1){
            editText.setHint("请输入费用时限");
        }else{
            editText.setHint("请输入缴纳费用");
        }
        dialog.show();
    }

    private void modifyCircleData(){
        loadWaitDialog();
        String isPush = mIsPush?"1":"2";
        HttpMethod.circleEdit(this,this,mGroupChatId,null,mContributionsMoney,mDayDuration,null,isPush);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if(method == HttpMethod.HTTP_CIRCLE_EDIT){// 修改缴费金额与费用时限,圈子名称，简介
            ToastUtil.makeLongText(this,"修改成功");
            String isPush = mIsPush?"1":"2";
            EventBus.getDefault().post(new MemberManagerEvent(mContributionsMoney,mDayDuration,isPush));
        }
    }

    private void getIntentData(){
        Intent intent = getIntent();
        mCircleId = intent.getStringExtra("circleId");
        mGroupChatId = intent.getStringExtra("circleChatId");
        mContributionsMoney = intent.getStringExtra("money");
        mDayDuration = intent.getStringExtra("duration");
        String isPush = intent.getStringExtra("isPush");
        mIsPush = "1".equals(isPush)?true:false;
    }

    public static void startActivity(Activity activity,String circleId,String circleChatId,String money,String duration,String isPush){
        Intent intent = new Intent(activity,MemberManagerActivity.class);
        intent.putExtra("circleId",circleId);
        intent.putExtra("circleChatId",circleChatId);
        intent.putExtra("money",money);
        intent.putExtra("duration",duration);
        intent.putExtra("isPush",isPush);
        activity.startActivity(intent);
    }
}
